package danny.hoi.controller;

import com.github.pagehelper.PageInfo;
import danny.hoi.pojo.Result;
import danny.hoi.pojo.TbCategory;
import danny.hoi.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 返回全部的分类
     * @return
     */
    @GetMapping("/findAll")
    public List<TbCategory> findAll1Cate() {
        return categoryService.findAll();
    }


    @GetMapping("/findCategoryById/{id}")
    public TbCategory findCategoryById(@PathVariable Integer id) {
        return categoryService.findCategoryById(id);
    }


    @PostMapping("/save")
    public Result save(@RequestBody TbCategory category) {

        try {
            categoryService.add(category);
            return Result.ok("添加成功了！");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.fail("添加失败了！");
    }


    @PostMapping("/update")
    public Result update(@RequestBody TbCategory category) {

        try {
            categoryService.update(category);
            return Result.ok("修改成功了！");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.fail("修改失败了！");
    }

    @GetMapping("/deleteList")
    public Result deleteList(Integer[] ids){
        try {
            categoryService.deleteList(ids);
            return Result.ok("删除成功了！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败了!");
    }

    @GetMapping("/findOne/{id}")
    public TbCategory findOne(@PathVariable Integer id){
        return categoryService.findOne(id);
    }
}
